/*
 * MorseTest.java
 *
 * Created on May 15, 2008, 2:09 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;
import java.util.*;

public class MorseTest extends Form implements CommandListener {

    private static Command transCmd = new Command("Translate", Command.OK, 1);
    private static Command playCmd = new Command("Play", Command.OK, 1);
    private static Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
    
    private MorseME midlet;
    private TextField tf = new TextField("Org : ", "", 160, TextField.ANY);
    private StringItem sitem = new StringItem("Translated : ", "");
    private MainMenu menu;
    private String mStr = "";

    public MorseTest(MainMenu menu, MorseME midlet) {

        super("MorseME Testing");
        this.menu = menu;

        this.midlet = midlet;
        
        this.addCommand(transCmd);
        this.addCommand(playCmd);
        this.addCommand(cancelCmd);
        this.setCommandListener(this);

        this.append(tf);
        this.append(sitem);

    }

    public void refresh() {
        tf.setString("");
        sitem.setText("");
    }

    public void commandAction(Command command, Displayable displayable) {

        if (command == playCmd) {



            if (tf.getString().length() > 0) {

                if (sitem.getText().length() > 0) {

                    sitem.setText("");
                    mStr = MorseProcess.translate(tf.getString());
                    sitem.setText(mStr);
                } else {
                    mStr = MorseProcess.translate(tf.getString());
                    sitem.setText(mStr);
                }



                if (mStr != null) {

                    //for (int i = 0; i < 1; i++) {
                        //try {
                            //if (i > 0) {
                            //Thread.sleep(20000);
                            //}
                            midlet.play(mStr);

                        //} catch (InterruptedException ex) {

                        //    ex.printStackTrace();
                        //}
                    //}
                }
            }

        } else if (command == cancelCmd) {

            midlet.stop();
            Display.getDisplay(MorseME.getMIDlet()).setCurrent(menu);

        } else if (command == transCmd) {

            if (tf.getString().length() > 0) {

                if (sitem.getText().length() > 0) {

                    sitem.setText("");
                    mStr = MorseProcess.translate(tf.getString());
                    sitem.setText(mStr);
                } else {
                    mStr = MorseProcess.translate(tf.getString());
                    sitem.setText(mStr);
                }
            }


        }

    }

    public MorseTest() {
        super("");
        try {
            jbInit();
        } catch (Exception e) {
        }
    }

    void jbInit() throws Exception {
    }
}

